<template>
	<view>
		<view class="content">
			<img src="@/static/images/home/home_bottom.png" class="top_img" />
			<view class="current-declaration" v-if="currentDeclaration">{{ currentDeclaration }}</view>
			<view class="report-entry" @click="goReport">申报入口 ></view>
		
			<view class="menus">
				<view class="menu-list">
					<view class="menu-1" v-for="(item, index) in menuList" :key="index" @click="goDetail(item)">
						<img :src="item.img" />
						<view class="menu-name">{{ item.name }}</view>
					</view>
				</view>
				<view class="message" @click="toMessage">
					<view class="message-num">您有{{ noReadList.length || 0 }}条消息提醒</view>
					<image src="@/static/images/home/icon_right.png"></image>
				</view>
			</view>
		</view>
		<view class="latest-news">
			<view class="news-header">
				<view class="title">最新动态</view>
				<view class="more" @click="goMore">查看更多></view>
			</view>
			<view class="news-list">
				<view class="new-1" v-for="item in newsList" @click="toDetail(item)">
					<view class="img">
						<image src="@/static/images/task-selected.png"></image>
					</view>
					<view class="new-content">
						<view class="title">
							{{ item.title }}
						</view>
						<view class="time"> 发布时间：{{ item.createTime }} </view>
					</view>
				</view>
			</view>
		</view>
		<!-- 在线咨询 -->
		<image @click="toOnlineServe" class="serve" src="@/static/images/serve.png"></image>
		<view style="width: 100%; height: 30rpx; background: none"></view>
	</view>
</template>

<script>
	// AES加密
	import {
		aesEncrypt
	} from "@/utils/aes";
	// 通用验证方法
	import {
		checkPhone
	} from "@/utils/common";
	// 单个页面所需接口
	import {
		getInfo
	} from "@/api/user";
	import {
		getIndusnotificationManagementList,
		getCurrentDeclarationSystem,
		isReported,
	} from "@/api/home";
	import {
		getMessageList
	} from "@/api/workBench";
	export default {
		data() {
			return {
				menuList: [{
						name: "通知通告",
						url: "/packageA/notice/index",
						img: require("@/static/images/home/icon1.png"),
					},
					{
						name: "材料示范",
						url: "/packageA/reportGuide/index",
						img: require("@/static/images/home/icon2.png"),
					},
					{
						name: "评审结果",
						url: "/packageA/reviewResult/index",
						img: require("@/static/images/home/icon3.png"),
					},
				],
				newsList: [],
				currentDeclaration: "",
				currentDeclarationId: "",
				noReadList: [], // 未读消息
				timer: null,
				userInfo: {},
				currentDeclarationObj: {},
			};
		},
		methods: {
			initData() {
				// this.getUserInfo();
				this.getIndusnotificationManagementList();
				this.getCurrentDeclaration();
				this.getMessageList();
				// this.timer = setInterval(() => {
				// 	this.getMessageList();
				// }, 60000);
			},
			toOnlineServe() {
				uni.navigateTo({
					url: '/pages/home/onlineService'
				})
			},
			getMessageList() {
				getMessageList({
					isRead: 0,
					current: 1,
					size: 9999,
				}).then((res) => {
					if (res.code == 200) {
						this.noReadList = res.data.records;
					} else {
						uni.showToast({
							title: res.msg,
							icon: "none",
						});
					}
				});
			},
			getUserInfo() {
				getInfo().then((res) => {
					if (res.code == 200) {
						// console.log(res);
						uni.setStorageSync("userInfo", res.data.user);
					} else {
						uni.showToast({
							title: res.msg,
							icon: "none",
						});
					}
				});
			},
			async getCurrentDeclaration() {
				let res = await getCurrentDeclarationSystem();
				if (res.data) {
					this.currentDeclarationObj = res.data;
					this.currentDeclaration = res.data.name;
					this.currentDeclarationId = res.data.id;
				}
			},
			getIndusnotificationManagementList() {
				getIndusnotificationManagementList({
					size: 10,
					current: 1,
					type: '1',
				}).then((res) => {
					if (res.code == 200) {
						// console.log(res);
						this.newsList = res.data.records;
					} else {
						uni.showToast({
							title: res.msg,
							icon: "none",
						});
					}
				});
			},
			goDetail(item) {
				uni.navigateTo({
					url: item.url,
				});
			},
			async goReport() {
				this.userInfo = uni.getStorageSync("userInfo");
				if (this.currentDeclaration) {
					if (this.userInfo.roles[0].roleKey == "APPLICANT") {
						if (this.currentDeclarationObj.status == 2) {
							let res = await isReported({
								userId: this.userInfo.userId,
								declarationSystemId: this.currentDeclarationId,
							});
							if (res.code == 200 && res.data) {
								if (res.data.status == 1) {
									uni.showToast({
										title: "当前体系申报正在审核中，请耐心等待",
										icon: "none",
									});
								} else if (res.data.status == 2) {
									uni.showToast({
										title: "当前体系申报审核未成功，请重新填写",
										icon: "none",
									});
									setTimeout(() => {
										uni.setStorageSync(
											"currentDeclarationId",
											this.currentDeclarationId
										);
										uni.navigateTo({
											url: "/packageA/report/index?id=" + res.data.id +
												"&edit=1",
										});
									}, 2000);
								} else if (res.data.status == 3) {
									// uni.showToast({
									// 	title: "请前往我的申报进行填报",
									// 	icon: "none",
									// });
									uni.showModal({
										title: "提示",
										content: "是否前往进行填报?",
										confirmText: "前往",
										success: function(res) {
											if (res.confirm) {
												// console.log("用户点击确定");
												uni.navigateTo({
													url: "/packageB/myDeclare/index"
												});
											} else if (res.cancel) {
												console.log("用户点击取消");
											}
										},
									});
								}
							} else {
								uni.setStorageSync(
									"currentDeclarationId",
									this.currentDeclarationId
								);
								uni.navigateTo({
									url: "/packageA/report/index",
								});
							}
						} else if (this.currentDeclarationObj.status == 1) {
							uni.showToast({
								title: "填报还未开始",
								icon: "none",
							});
						} else if (this.currentDeclarationObj.status == 3) {
							uni.showToast({
								title: "专家评审中，不允许申报",
								icon: "none",
							});
						} else if (this.currentDeclarationObj.status == 4) {
							uni.showToast({
								title: "分数公示期，不允许申报",
								icon: "none",
							});
						} else if (this.currentDeclarationObj.status == 5) {
							uni.showToast({
								title: "该申报已经结束",
								icon: "none",
							});
						}
					} else {
						uni.showToast({
							title: "当前用户角色不能申报",
							icon: "none",
						});
					}
				} else {
					uni.showToast({
						title: "暂无申报体系，不可填报",
						icon: "none",
					});
				}
			},
			goMore() {
				uni.navigateTo({
					url: "/packageA/latestNews/index",
				});
			},
			register() {
				uni.navigateTo({
					url: `./register`,
				});
			},
			toDetail(data) {
				uni.navigateTo({
					url: `/packageA/latestNews/detail?id=${data.id}`,
				});
			},
			toMessage() {
				uni.navigateTo({
					url: `/packageB/messageNotification/index?showNoRead=true`,
				});
			},
		},
	};
</script>

<style lang="scss" scoped>
	.serve {
		width: 130rpx;
		height: 124rpx;
		position: fixed;
		right: 20rpx;
		bottom: 105rpx;
	}
	.content {
		position: relative;
		width: 100%;
		height: 700rpx;
		background: #ffffff;

		.top_img {
			width: 750rpx;
			height: 428rpx;
		}

		.report-entry {
			position: absolute;
			top: 200rpx;
			left: 55rpx;
			// z-index: 11;
			width: 190rpx;
			height: 68rpx;
			background: #e97c34;
			border: 6rpx solid #f9bd8e;
			border-radius: 33rpx;
			color: #ffffff;
			line-height: 56rpx;
			text-align: center;
		}

		.current-declaration {
			position: absolute;
			top: 116rpx;
			left: 55rpx;
			color: #ffffff;
			font-size: 30rpx;
			margin-bottom: 10rpx;
			background-color: rgba(0, 0, 0, 0.5);
			padding: 4rpx 16rpx;
			border-radius: 10rpx;
		}

		.menus {
			position: absolute;
			top: 314rpx;
			left: 35rpx;
			// z-index: 10;
			width: 680rpx;

			.menu-list {
				background: #fff;
				border-radius: 31rpx;
				font-size: 24rpx;
				text-align: center;
				// padding-top: 120rpx;
				height: 217rpx;
				padding: 0 50rpx;
				box-shadow: 0px 3rpx 6rpx 0px rgba(124, 144, 222, 0.25);
				display: flex;
				align-items: center;
				justify-content: space-between;

				.menu-1 {
					text-align: center;

					image {
						width: 102rpx;
						height: 102rpx;
					}

					.menu-name {
						font-size: 28rpx;
						font-weight: 500;
						color: #000000;
						line-height: 54rpx;
					}
				}
			}

			.message {
				width: 100%;
				height: 74rpx;
				background: #fef7da;
				border-radius: 37rpx;
				margin-top: 50rpx;
				padding: 0 36rpx;
				display: flex;
				align-items: center;
				justify-content: space-between;

				.message-num {
					padding-left: 48rpx;
					background: url("@/static/images/home/icon_message.png") left center no-repeat;
					background-size: 32rpx 32rpx;
					color: #e8b042;
				}

				image {
					width: 15rpx;
					height: 28rpx;
				}
			}
		}
	}

	.latest-news {
		background: #ffffff;
		margin-top: 20rpx;
		padding: 0 35rpx;
		margin-bottom: 30rpx;

		.news-header {
			display: flex;
			align-items: center;
			justify-content: space-between;
			height: 80rpx;
			.title {
				font-size: 28rpx;
				color: #000000;
			}
			.more {
				font-size: 24rpx;
				color: #477ef5;
			}
		}

		.news-list {
			.new-1 {
				display: flex;
				align-items: center;
				justify-content: space-between;
				padding: 15rpx 0;
				border-bottom: 2rpx solid #f5f5f5;

				.img {
					width: 96rpx;
					height: 96rpx;

					image {
						width: 96rpx;
						height: 96rpx;
					}
				}

				.new-content {
					width: 540rpx;

					.title {
						font-size: 28rpx;
						color: #333333;
						height: 40rpx;
						line-height: 40rpx;
						white-space: nowrap;
						overflow: hidden;
						text-overflow: ellipsis;
					}

					.time {
						margin-top: 10rpx;
						font-size: 24rpx;
						color: #999999;
					}
				}
			}
		}
	}
</style>
